//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Eas.WebCrawler.Test.PlugIns {
    /// <summary>
    /// Summary description for ASPNetDetectorTest
    /// </summary>
    [TestClass]
    public class ASPNetDetectorTest {
        public ASPNetDetectorTest() {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() { TestDirectoryHelper.Setup(); }
        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() { TestDirectoryHelper.TearDown(); }
        //
        #endregion

        [TestMethod]
        /// <summary>Executes the TestExtension test.</summary>
        public void TestExtension() {
            TestFile("Test.html", true, RequestHelper.rMixedCaseAspNet);
        }

        [TestMethod]
        /// <summary>Executes the TestFile2 test.</summary>
        public void TestFile2() {
            TestFile("Test2.htm", false, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile3 test.</summary>
        public void TestFile3() {
            TestFile("Test3.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile4 test.</summary>
        public void TestFile4() {
            TestFile("Test4.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile5 test.</summary>
        public void TestFile5() {
            TestFile("Test5.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile6 test.</summary>
        public void TestFile6() {
            TestFile("Test6.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile7 test.</summary>
        public void TestFile7() {
            TestFile("Test7.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile8 test.</summary>
        public void TestFile8() {
            TestFile("Test8.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile9 test.</summary>
        public void TestFile9() {
            TestFile("Test9.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestFile91 test.</summary>
        public void TestFile91() {
            TestFile("Test91.htm", true, RequestHelper.rGoogle);
        }

        [TestMethod]
        /// <summary>Executes the TestNotText test.</summary>
        public void TestNotText() {
            string filename = "Test9.htm";
            using (Eas.WebCrawler.PlugIns.TemporaryStorage.MemoryStorage store = CreateStorage(filename)) {
                Assert.IsFalse(Eas.WebCrawler.PlugIns.UrlProcessedHandlers.AspNetDetector.IsAspNet(new Eas.WebCrawler.Interfaces.UrlCompletedEventArgs(new TestProcessingState(2, RequestHelper.rGoogle, false, new System.IO.MemoryStream()), store)));
            }
        }


        private void TestFile(string filename, bool isAspNet, Eas.WebCrawler.Interfaces.Request request) {
            using (Eas.WebCrawler.PlugIns.TemporaryStorage.MemoryStorage store = CreateStorage(filename)) {
                Assert.AreEqual<bool>(isAspNet, Eas.WebCrawler.PlugIns.UrlProcessedHandlers.AspNetDetector.IsAspNet(new Eas.WebCrawler.Interfaces.UrlCompletedEventArgs(new TestProcessingState(2, request, true, new System.IO.MemoryStream()), store)));
            }
        }

        private Eas.WebCrawler.PlugIns.TemporaryStorage.MemoryStorage CreateStorage(string filename) {
            string fullFile = TestDirectoryHelper.Name(TestDirectory.Source, filename);
            Eas.WebCrawler.PlugIns.TemporaryStorage.MemoryStorage storage = new Eas.WebCrawler.PlugIns.TemporaryStorage.MemoryStorage();
            using (System.IO.TextWriter writer = storage.GetTextWriter()) {
                using (System.IO.TextReader reader = new System.IO.StreamReader(fullFile)) {
                    Eas.WebCrawler.Common.IO.StreamCopier.CopyTextStreams(reader, writer, 500);
                }
            }
            return storage;
        }
    }
}
